<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of users_controller
 *
 * @author Youness.MONDIR-SCx@
 */

class CompaniesController extends AppController {

	var $name = 'Companies';
       function  beforeFilter() {
            parent::beforeFilter();
            $this->Auth->autoRedirect = false;
            $this->set('active',3);
        }
        function filterCompanies(){
            $conditions=array();$c=array();$result=array();
            if($this->data){
                $data=  $this->data['Company'];
                (strlen($data['title'])>0)?$c['title Like']='%'.$data['title'].'%':$c['title Like']='%*%';
                (strlen($data['site'])>0)?$c['site Like']='%'.$data['site'].'%':$c['site Like']='%*%';
                (strlen($data['department'])>0)?$c['department Like']='%'.$data['department'].'%':$c['department Like']='%*%';
                (strlen($data['activity'])>0)?$c['activity Like']='%'.$data['activity'].'%':$c['activity Like']='%*%';
                (strlen($data['code'])>0)?$c['code Like']='%'.$data['code'].'%':$c['code Like']='%*%';
                (strlen($data['country'])>0)?$c['country Like']='%'.$data['country'].'%':$c['country Like']='%*%';
                $conditions['OR']=$c;
                $result=$this->Company->find('list',array(
                    'fields'=>array('fullRow'),
                    'conditions'=>$conditions
                ));
                
            }
            $this->set('result',$result);
                    
        }

        function getCountries(){
            $t=$this->Company->Country->find('list');
            
            $this->set('countries',$t);
        }

        function getCompaniesForCountry($countryId){
             $t=$this->Company->find('all',array(
                'recursive'=>-1,
                 'conditions'=>array('country_id'=>$countryId),
                'fields'=>array('DISTINCT title'),
            ));
            $r=Set::extract($t,"/Company/title");
            $this->set('companies',$r);
        }

        function getSitesForCompany($companyTitle){
             $t=$this->Company->find('all',array(
                'recursive'=>-1,
                 'conditions'=>array('Company.title'=>$companyTitle),
                'fields'=>array('DISTINCT site'),
            ));
            $r=Set::extract($t,"/Company/site");
            $this->set('sites',$r);
        }

        function getDepartmentsForSite($site){
            $t=$this->Company->find('all',array(
                'recursive'=>-1,
                 'conditions'=>array('Company.site'=>$site),
                'fields'=>array('DISTINCT department'),
            ));
            $r=Set::extract($t,"/Company/department");
            $this->set('departments',$r);
        }

        function getCompanyId($dep,$site,$comp,$country){
            $t=$this->Company->find('first',array(
               'recursive'=>-1,
           'conditions'=>array(
                   'AND'=>array(
                   'department'=>$dep,
                   'site'=>$site,
                   'title'=>$comp,
                   'country_id'=>$country
               )),
                'fields'=>'id'
            ));
    $this->set('t',$t['Company']['id']);
        }
        
        
        
        function index() {
		$this->Company->recursive = 0;
		$this->set('companies', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid company', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('company', $this->Company->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Company->create();
			if ($this->Company->save($this->data)) {
				$this->Session->setFlash(__('The company has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The company could not be saved. Please, try again.', true));
			}
		}
                $this->set('countries',$this->Company->Country->find('list'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid company', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Company->save($this->data)) {
				$this->Session->setFlash(__('The company has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The company could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Company->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for company', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Company->delete($id)) {
			$this->Session->setFlash(__('Company deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Company was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

}

?>
